package com.mediatek.factorymode;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.widget.TextView;
import java.util.ArrayList;

import android.telephony.TelephonyManager;
import android.telephony.TelephonyManager.MultiSimVariants;
import android.provider.Settings;
import android.hardware.Camera;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ShortcutManager;
import android.content.pm.ShortcutInfo;
import android.graphics.drawable.Icon;
import android.os.Build;

public class Utils{
	/** note: arrayOfInt size must match strArray
	/*		  arrayOfInt list must match strArray
	*/
	public String pkg=null;
	public ArrayList<String> localArrayList = new ArrayList();
	public static int[] arrayOfInt = {
		R.string.touchscreen_name,
		R.string.lcd_name,
		R.string.battery_name,
		R.string.KeyCode_name,
		R.string.speaker_name,
		R.string.microphone_name,
		R.string.earphone_name,
		R.string.headset_name,
		R.string.wifi_name,
		//R.string.ethernet_name,
		R.string.bluetooth_name,
		R.string.backlight_name,
		R.string.memory_name,
		R.string.otg_udisk_name,
		R.string.camera_name,
		R.string.subcamera_name,
		//R.string.flashlight,
		R.string.sim_name,
		R.string.telephone_name,
		//R.string.led_name,
		R.string.fmradio_name,
		R.string.vibrator_name,
		R.string.gsensor_name,
		//R.string.gsensorcali_name,
		R.string.lsensor_name,
		R.string.psensor_name,
		//R.string.msensor_name,
		R.string.gps_name,
		R.string.device_info,
		R.string.version,
	//	R.string.googlekey
	};

	public String[] strArray = {
		"ro.mid.touchscreen_name",
		"ro.mid.lcd_name",
		"ro.mid.battery_name",
		"ro.mid.KeyCode_name",
		"ro.mid.speaker_name",
		"ro.mid.microphone_name",
		"ro.mid.earphone_name",
		"ro.mid.headset_name",
		"ro.mid.wifi_name",
		//"ro.mid.ethernet_name",
		"ro.mid.bluetooth_name",
		"ro.mid.backlight_name",
		"ro.mid.memory_name",
		"ro.mid.otg_udisk_name",
		"ro.mid.camera_name",
		"ro.mid.subcamera_name",
		//"ro.mid.flashlight",
		"ro.mid.sim_name",
		"ro.mid.telephone_name",
		//"ro.mid.led_name",
		"ro.mid.fmradio_name",
		"ro.mid.vibrator_name",
		"ro.mid.gsensor_name",
		//"ro.mid.gsensorcali_name",
		"ro.mid.lsensor_name",
		"ro.mid.psensor_name",
		//"ro.mid.msensor_name",
		"ro.mid.gps_name",
		"ro.mid.device_info",
		"ro.mid.version",
//		"ro.mid.googlekey"
	};

 	public static void SetPreferences(Context paramContext, SharedPreferences paramSharedPreferences, int paramInt, String paramString){
    	String str = paramContext.getResources().getString(paramInt);
    	SharedPreferences.Editor localEditor = paramSharedPreferences.edit();
    	localEditor.putString(str, paramString);
    	localEditor.commit();
  	}

  	public ArrayList<String> getListArray(Resources res){
  		for(int i=0; i < strArray.length; i++){
  			if(android.os.SystemProperties.get(strArray[i],"1").equals("1")){
  				String str1 = res.getString(arrayOfInt[i]);
  				localArrayList.add(str1);
  			}
  		}
  		return localArrayList;
   	}

   	public String getTestPkgName(Resources res, String str1){
   		if (res.getString(R.string.speaker_name).equals(str1))
			pkg = "com.mediatek.factorymode.audio.AudioTest";
		else if (res.getString(R.string.battery_name).equals(str1))
			pkg = "com.mediatek.factorymode.BatteryLog";
		else if (res.getString(R.string.touchscreen_name).equals(str1))
			//pkg = "com.mediatek.factorymode.touchscreen.TouchPadTest";
			//pkg = "com.mediatek.factorymode.touchscreen1.TouchScreenTest";
			pkg = "com.mediatek.factorymode.touchscreen2.BlockTest";
		else if (res.getString(R.string.camera_name).equals(str1))
			pkg = "com.mediatek.factorymode.camera.CameraTest";
		else if (res.getString(R.string.wifi_name).equals(str1))
			pkg = "com.mediatek.factorymode.wifi.WiFiTest";
		else if (res.getString(R.string.ethernet_name).equals(str1))
			pkg = "com.mediatek.factorymode.ethernet.EthernetTest";
		else if (res.getString(R.string.bluetooth_name).equals(str1))
			pkg = "com.mediatek.factorymode.bluetooth.Bluetooth";
		else if (res.getString(R.string.headset_name).equals(str1))
			pkg = "com.mediatek.factorymode.headset.HeadSet";
		else if (res.getString(R.string.earphone_name).equals(str1))
			pkg = "com.mediatek.factorymode.earphone.Earphone";
		else if (res.getString(R.string.vibrator_name).equals(str1))
			pkg = "com.mediatek.factorymode.vibrator.Vibrator";
		else if (res.getString(R.string.telephone_name).equals(str1))
			pkg = "com.mediatek.factorymode.signal.Signal";
		else if (res.getString(R.string.gps_name).equals(str1))
			pkg = "com.mediatek.factorymode.gps.GPS";
		else if (res.getString(R.string.backlight_name).equals(str1))
			pkg = "com.mediatek.factorymode.backlight.BackLight";
		else if (res.getString(R.string.memory_name).equals(str1))
			pkg = "com.mediatek.factorymode.memory.Memory";
		else if (res.getString(R.string.microphone_name).equals(str1))
			pkg = "com.mediatek.factorymode.microphone.MicRecorder";
		else if (res.getString(R.string.gsensor_name).equals(str1))
			pkg = "com.mediatek.factorymode.sensor.GSensor";
		else if (res.getString(R.string.gsensorcali_name).equals(str1))
			pkg = "com.mediatek.factorymode.sensor.GSensorCali";
		else if (res.getString(R.string.msensor_name).equals(str1))
			pkg = "com.mediatek.factorymode.sensor.MSensor";
		else if (res.getString(R.string.lsensor_name).equals(str1))
			pkg = "com.mediatek.factorymode.sensor.LSensor";
		else if (res.getString(R.string.psensor_name).equals(str1))
			pkg = "com.mediatek.factorymode.sensor.PSensor";
		else if (res.getString(R.string.otg_udisk_name).equals(str1))
			pkg = "com.mediatek.factorymode.otg.OtgUdisk";
		else if (res.getString(R.string.sdcard_name).equals(str1))
			pkg = "com.mediatek.factorymode.sdcard.SDCard";
		else if (res.getString(R.string.fmradio_name).equals(str1))
			pkg = "com.mediatek.factorymode.fmradio.FmTest2";
		else if(res.getString(R.string.device_info).equals(str1))
			pkg = "com.mediatek.factorymode.deviceinfo.DeviceInfo";
		else if (res.getString(R.string.KeyCode_name).equals(str1))
			pkg = "com.mediatek.factorymode.KeyCode";
		else if (res.getString(R.string.lcd_name).equals(str1))
			pkg = "com.mediatek.factorymode.lcd.LCD";
		else if (res.getString(R.string.sim_name).equals(str1))
			pkg = "com.mediatek.factorymode.simcard.SimTest";
		else if (res.getString(R.string.subcamera_name).equals(str1))
			pkg = "com.mediatek.factorymode.camera.SubCamera";
		else if (res.getString(R.string.led_name).equals(str1))
			pkg = "com.mediatek.factorymode.led.Led";
		else if (res.getString(R.string.version).equals(str1))
			pkg = "com.mediatek.factorymode.version.version";
		else if (res.getString(R.string.flashlight).equals(str1))
			pkg = "com.mediatek.factorymode.flashlight.FlashLight";
		else if (res.getString(R.string.googlekey).equals(str1))
			pkg = "com.mediatek.factorymode.googlekey.googlekey";

		return pkg;
   	}

	public static void setTestItem(TextView testItem, int strId, Resources res){
		testItem.setText(res.getString(strId)+"  "+res.getString(R.string.testItem));
	}

	public static boolean mIsDsds() {
        MultiSimVariants config = TelephonyManager.getDefault().getMultiSimConfiguration();
        boolean mIsDsds = (config == MultiSimVariants.DSDS || config == MultiSimVariants.DSDA);
        return mIsDsds;
    }

    public static void putInt(Context mContext, String str, int i) {
    	Settings.System.putInt(mContext.getContentResolver(), str, i);
    }

    public static int getInt(Context mContext, String str) {
    	return Settings.System.getInt(mContext.getContentResolver(), str, 0);
    }

    public static boolean hasCamera() {
        int n = Camera.getNumberOfCameras();
        return (n > 0);
    }

    public static int getCameraNum() {
        int n = Camera.getNumberOfCameras();
        return n;
    }

    public static void addShortcut(Activity cx, String name) {
    	if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
    		ShortcutManager shortcutManager = (ShortcutManager) cx.getSystemService(Context.SHORTCUT_SERVICE);
        	if(shortcutManager != null && shortcutManager.isRequestPinShortcutSupported()){
        	  Intent shortcutInfoIntent = new Intent();
        	  shortcutInfoIntent.setClassName("com.mediatek.factorymode","com.mediatek.factorymode.MainActivity");
        	  shortcutInfoIntent.putExtra("shortcut", true);
        	  shortcutInfoIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        	  shortcutInfoIntent.setAction(Intent.ACTION_VIEW);

        	  ShortcutInfo info = new ShortcutInfo.Builder(cx, "open_report_id")
        	        .setIcon(Icon.createWithResource(cx, R.drawable.ic_launcher))
        	        .setShortLabel(name)
        	        .setIntent(shortcutInfoIntent)
        	        .build();
        	  shortcutManager.requestPinShortcut(info, null);
        	}
    	}else{
        	Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        	shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
        	shortcut.putExtra("duplicate", false);
        	Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(cx, R.drawable.ic_launcher);
        	shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);


        	Intent carryIntent = new Intent(Intent.ACTION_MAIN);
        	carryIntent.putExtra("name", name);
        	carryIntent.setClassName("com.mediatek.factorymode","com.mediatek.factorymode.MainActivity");
        	carryIntent.putExtra("shortcut", true);
        	carryIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        	shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, carryIntent);
        	cx.sendBroadcast(shortcut);
    	}
    }
}
